package com.guguskins.back.utils;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisCallback;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.stereotype.Component;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.TimeUnit;

/**
 * @date 2021/5/10 10:26
 */
@Component
public class RedisUtil {


    private static StringRedisTemplate redisTemplate;

    @Autowired
    public void setRedisTemplate(StringRedisTemplate redisTemplate) {
        RedisUtil.redisTemplate = redisTemplate;
    }

    public static void setKey(String key, String value) {
        redisTemplate.opsForValue().set(key, value);
    }

    public static Long setKey(String key, String value, int time, TimeUnit unit) {
        redisTemplate.opsForValue().set(key, value, time, unit);
        return redisTemplate.getExpire(key);
    }

    public static String getValue(String key) {
        ValueOperations<String, String> ops = redisTemplate.opsForValue();
        return ops.get(key);
    }

    public static void incr(String key) {
        redisTemplate.boundValueOps(key).increment(1);
    }

    public static void expire(String key, long timeout) {
        redisTemplate.expire(key, timeout, TimeUnit.MICROSECONDS);
    }

    public static Long getExpire(String key) {
        return redisTemplate.getExpire(key);
    }

    public static void deleteKey(String key) {
        redisTemplate.delete(key);
    }

    public static void deleteKeys(String keyPrefix) {
        Set<String> keys = redisTemplate.keys(keyPrefix + "*");
        if (!CollectionUtils.isEmpty(keys)) {
            redisTemplate.delete(keys);
        }
    }

    public static void setHashValue(String key, String hashKey, Object hashValue) {
        redisTemplate.opsForHash().put(key, hashKey, hashValue);
    }

    public static void setHashValue(String key, String hashKey, String hashValue, long timeout, TimeUnit unit) {
        if (hasKey(key)) {
            setHashValue(key, hashKey, hashValue);
        } else {
            setHashValue(key, hashKey, hashValue);
            redisTemplate.expire(key, timeout, unit);
        }
    }

    public static Map<String, String> getAllHash(String key) {
        return redisTemplate.execute((RedisCallback<Map<String, String>>) con -> {
            Map<byte[], byte[]> result = con.hGetAll(key.getBytes());
            if (CollectionUtils.isEmpty(result)) {
                return new HashMap<>(0);
            }

            Map<String, String> ans = new HashMap<>(result.size());
            for (Map.Entry<byte[], byte[]> entry : result.entrySet()) {
                ans.put(new String(entry.getKey()), new String(entry.getValue()));
            }
            return ans;
        });
    }

    public static String getHashValue(String key, String hashKey) {
        return (String) redisTemplate.opsForHash().get(key, hashKey);
    }

    public static Long getHashSize(String key) {
        return redisTemplate.opsForHash().size(key);
    }

    public static boolean isHashValue(String key, String hashKey) {
        return redisTemplate.opsForHash().hasKey(key, hashKey);
    }

    public static Long hincrby(String key, String hashKey, int increment) {
        return redisTemplate.opsForHash().increment(key, hashKey, increment);
    }

    public static void setSetValue(String key, String value) {
        redisTemplate.opsForSet().add(key, value);
    }

    public static Boolean isSetValue(String key, String hashKey) {
        if (hasKey(key)) {
            return redisTemplate.opsForSet().isMember(key, hashKey);
        }
        return false;
    }

    public static void deleteSetValue(String key, String value) {
        redisTemplate.opsForSet().remove(key, value);
    }

    public static void addOrIncrementZSet(String key, String value, double score) {
        redisTemplate.opsForZSet().incrementScore(key, value, score);
    }

    public static Set<String> reverseRangeByScore(String key, double min, double max, long offset, long count) {

        return redisTemplate.opsForZSet().reverseRangeByScore(key, min, max, offset, count);
    }

    public static Set<String> reverseRangeByScore(String key, double min, double max) {

        return redisTemplate.opsForZSet().reverseRangeByScore(key, min, max);
    }

    public static Boolean hasKey(String key) {
        return redisTemplate.hasKey(key);
    }

    public static List<String> range(String key) {
        return redisTemplate.opsForList().range(key, 0, -1);
    }

    public static List<String> range(String key, long start, long end) {
        return redisTemplate.opsForList().range(key, start, end);
    }

    public static void rightPush(String key, String value) {
        redisTemplate.opsForList().rightPush(key, value);
    }

    public static void rightPush(String key, String value, long timeout, TimeUnit unit) {
        redisTemplate.opsForList().rightPush(key, value);
        redisTemplate.expire(key, timeout, unit);
    }

    public static void rightPushAll(String key, List<String> values) {
        redisTemplate.opsForList().rightPushAll(key, values);
    }

    //校验redis中的key是否存在
    public boolean hashKey(String key) {
        return !StringUtils.isEmpty(key) && redisTemplate.hasKey(key);
    }

    public static Long size(String key) {
        return redisTemplate.opsForList().size(key);
    }

    public void delete(String key) {
        redisTemplate.delete(key);
    }

    public Long setString(String key, String value, Long expire, TimeUnit timeUnit) {
        ValueOperations<String, String> ops = redisTemplate.opsForValue();
        ops.set(key, value, expire, timeUnit);
        return redisTemplate.getExpire(key);
    }
}
